package com.Example;

import com.Example.Model.Cert.CertInfo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.client.RepChainJksClient;
import com.google.common.base.Charsets;
import com.google.common.io.Files;
import com.google.protobuf.Timestamp;
import com.google.protobuf.util.JsonFormat;
import com.protos.Peer;
import org.apache.commons.codec.binary.Hex;

import java.io.File;
import java.util.ArrayList;

import static java.lang.Thread.sleep;


/**
 * 注册账户与证书的示例
 * @author zyf
 */
public class SignUp {

    private static String host = "localhost:8081";
    private static RepChainJksClient client = new RepChainJksClient(host,"jks/121000005l35120456.node1.jks", "123", "121000005l35120456.node1");
    private static Peer.CertId certId = Peer.CertId.newBuilder().setCreditCode("121000005l35120456").setCertName("node1").build(); // 签名ID
    // 证书管理合约ID
    private static Peer.ChaincodeId contractCertId = Peer.ChaincodeId.newBuilder().setChaincodeName("ContractCert").setVersion(1).build();

    public static void main(String[] args) throws Exception{

        // 注册账户
        ArrayList<String > CertNames = new ArrayList<>();
        CertNames.add("cert1");
        CertNames.add("cert2");
        // 也可以自己构造一个javaBean，然后使用Json序列化
        Peer.Signer signer = Peer.Signer.newBuilder()
                .setName("zyf")
                .setCreditCode("121000005l35120567")
                .setMobile("13812345678")
                .addAllCertNames(CertNames)
                .build();
//        Map usrCer = new HashMap<String, String>();
//        usrCer.put(base64Cer, base64Info);
        Peer.Transaction signUpSigner = client.createTransactionForInvoke(certId, contractCertId, "SignUpSigner" , JsonFormat.printer().print(signer));
        String signUpSignerStr = Hex.encodeHexString(signUpSigner.toByteArray());
        JSONObject signUpSignerResult = client.postTranByString(JSON.toJSONString(signUpSignerStr));
        System.out.println(signUpSignerResult);
        sleep(10000);
        //注册用户证书
        String pemCer = Files.asCharSource(new File("jks/qz.cer"), Charsets.UTF_8).read();
//      pemCrt = X509Factory.BEGIN_CERT + Base64.encodeBase64String(certificate.getEncoded()) + X509Factory.END_CERT;
        long regMillis = System.currentTimeMillis() + 8*3600*1000; //注册时间
        long unRegMillis = System.currentTimeMillis() + 2*8*3600*1000;  // 注销时间
        Timestamp regTime = Timestamp.newBuilder().setSeconds(regMillis/1000).setNanos((int)(regMillis % 1000) * 1000000).build();
        Timestamp unRegTime = Timestamp.newBuilder().setSeconds(unRegMillis/1000).setNanos((int)(unRegMillis % 1000) * 1000000).build();
        // 也可以自己构造一个javaBean，然后使用Json序列化
        Peer.Certificate certificate = Peer.Certificate.newBuilder()
                .setCertificate(pemCer)
                .setAlgType("SHA1withECDSA")
                .setCertValid(true)
                .setRegTime(regTime)
                .setUnregTime(unRegTime)
                .build();
        // 也可以自己构造一个javaBean，然后使用Json序列化
        CertInfo certInfo = new CertInfo("121000005l35120567","cert3",JSON.parse(JsonFormat.printer().print(certificate)));
        Peer.Transaction signUpCer = client.createTransactionForInvoke(certId, contractCertId, "SignUpCert" ,JSON.toJSONString(certInfo));
        String signUpCerStr = Hex.encodeHexString(signUpCer.toByteArray());
        JSONObject signUpCerResult = client.postTranByString(JSON.toJSONString(signUpCerStr));
        System.out.println(signUpCerResult);
    }
}
